package zekezang.org;

import java.util.ArrayList;
import java.util.List;

import common.utils.activity.ActivityQueue;
import common.utils.activity.ActivityUtils;
import common.utils.database.DatabaseHelper;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BookMarkActivity extends Activity {

	private RelativeLayout menuLayout = null;

	private common.utils.activity.MainMenu mainMenu = null;
	private ListView listView = null;

	private ImageButton title_back_button = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ActivityUtils.getInstance().setScreenOrientation(this);

		setContentView(R.layout.book_mark);

		ActivityQueue.activityList.add(this);

		listView = (ListView) findViewById(R.id.bookListView);

		menuLayout = (RelativeLayout) findViewById(R.id.menu_layout);
		menuLayout.setVisibility(View.VISIBLE);

		footerSetting();

		title_back_button = (ImageButton) findViewById(R.id.title_back_button);

		title_back_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				BookMarkActivity.this.finish();
			}
		});

	}

	private void footerSetting() {
		mainMenu = common.utils.activity.MainMenu.getInstance(this, this, menuLayout, null, "47", null);
		// menu向上键
		ImageButton menu_up_button = (ImageButton) menuLayout.findViewById(R.id.menu_up_button);
		menu_up_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnKeyDown();
			}
		});
		// menu主页
		final ImageButton menu_home_button = (ImageButton) menuLayout.findViewById(R.id.menu_home_button);
		menu_home_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance().to_home_Transition(BookMarkActivity.this, getApplicationContext(), MainActivity.class);
			}
		});
		// menu后退
		final ImageButton menu_back_button = (ImageButton) menuLayout.findViewById(R.id.menu_back_button);
		menu_back_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance().back_leftOUT_rightIN_Transition(BookMarkActivity.this, getApplicationContext());
			}
		});
		// menu前进
		final ImageButton menu_go_button = (ImageButton) menuLayout.findViewById(R.id.menu_go_button);
		menu_go_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mainMenu.responseOnTouch();
				ActivityUtils.getInstance().forward_leftOUT_rightIN_Transition(BookMarkActivity.this, getApplicationContext());
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		listView.setAdapter(new BookListViewAdapter(this, getBookList()));
	}

	private List<BookMarkVO> getBookList() {
		List<BookMarkVO> list = new ArrayList<BookMarkVO>();
		String sql = "select * from book order by id desc";
		SQLiteDatabase sqLiteDatabase = new DatabaseHelper(this, "book_DB").getReadableDatabase();
		try {
			Cursor cursor = sqLiteDatabase.rawQuery(sql, new String[] {});
			BookMarkVO bookMarkVO = null;
			while (cursor.moveToNext()) {
				bookMarkVO = new BookMarkVO();
				bookMarkVO.setId(cursor.getInt(cursor.getColumnIndex("id")));
				bookMarkVO.setUriDescription(cursor.getString(cursor.getColumnIndex("uriDescription")));
				bookMarkVO.setUri(cursor.getString(cursor.getColumnIndex("uri")));
				list.add(bookMarkVO);
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			sqLiteDatabase.close();
		}

		return list;
	}

	public class BookListViewAdapter extends BaseAdapter {
		private Context context;
		private View listContentView = null;
		private List<BookMarkVO> bookMarkVOs = null;

		public BookListViewAdapter(Context context, List<BookMarkVO> bookMarkVOs) {
			this.context = context;
			this.bookMarkVOs = bookMarkVOs;
		}

		@Override
		public int getCount() {
			return bookMarkVOs.size();
		}

		@Override
		public Object getItem(int arg0) {
			return bookMarkVOs.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		private TextView booktitle = null;
		private TextView bookurl = null;
		private Button button = null;

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			listContentView = LayoutInflater.from(context).inflate(R.layout.book_list_content, null);
			booktitle = (TextView) listContentView.findViewById(R.id.booktitle);
			bookurl = (TextView) listContentView.findViewById(R.id.bookurl);
			button = (Button) listContentView.findViewById(R.id.bookDelete);
			BookMarkVO vo = bookMarkVOs.get(arg0);

			booktitle.setText(vo.getUriDescription());
			bookurl.setText(vo.getUri());
			button.setTag(vo.getId());

			final Integer id = vo.getId();
			button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					deleteBookMark(id);
					listView.setAdapter(new BookListViewAdapter(BookMarkActivity.this, getBookList()));
				}
			});

			final String uri = vo.getUri();
			booktitle.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View arg0, MotionEvent arg1) {
					// TODO Auto-generated method stub
					ActivityUtils.getInstance().leftIN_rightOUT_Transition(BookMarkActivity.this, getApplicationContext(), WebviewActivity.class,
							"uri", uri);
					return false;
				}
			});

			return listContentView;
		}
	}

	private SQLiteDatabase sqLiteDatabase = null;

	private void deleteBookMark(Integer i) {
		sqLiteDatabase = new DatabaseHelper(this, "book_DB").getWritableDatabase();
		String sql = "delete from book where id = " + i;
		sqLiteDatabase.execSQL(sql);
		sqLiteDatabase.close();
	}
}
